<?PHP

class forum_class
{
	
//	public function __get($p) 
//	{ 
//		return $this->$p; 
//	} 
//	
//	public function __set($p, $v) 
//	{ 
//		$this->$p = $v; 
//	}
	
	function display_all_boards ($type,$header)
	{
		global $db;
		
		echo "<br><h2>$header</h2>";

		$options = array();
		
		$query = "SELECT * FROM `forum_catagories` WHERE `cat`='$type' ORDER BY `name`";
		
		if ($result = $db->query($query)) {
		
		    /* fetch associative array */
		    while ($row = $result->fetch_assoc()) {
		    	$cat_id = $row['id'];
		        $options["/forum/$cat_id/"] = $row['name'];
		    }
		
		    /* free result set */
		    $result->close();
		}
		    
		    
		echo '<p>';
		echo player_options($options);
		echo '</p>';
	}
	
	
	
	function load_forum_data($board)
	{
		global $db;
		$this->board = $board;
		$this->board_info = $db->return_first_result("SELECT * FROM `forum_catagories` WHERE `id`='$board'");
		$this->type = $this->board_info['cat'];
		//display_var($this->board_info);
		return true;
	}
	
	
	function display_all_posts ()
	{
		global $db;
		
		$board = $this->board;
		$board_info = $this->board_info;
		$type = $this->type;
		
		$header = $board_info['name'];
		$type = $board_info['cat'];
		
		$output = "<h1>Forum: $header</h1>";
		
		if ($type == 'trade')
		{
			$output .= '<table 
		 cellpadding="2" cellspacing="2">
		  <tbody>
		    <tr>
		      <td><small><B>Item</B></small></td>
		      <td>&nbsp;</td>
		      <td><small><B>Price</B> (per item)</small></td>
		      <td><small><B>Total Price</B></small></td>
		      <td><small><B>Seller</B></small></td>
		      <td>&nbsp;</td>
		    </tr>

			';
		}
		else 
		{
			$output .= '<table 
		 cellpadding="6" cellspacing="6">
		  <tbody>
		    <tr>
		      <td colspan=3>&nbsp;</td>
		    </tr>

			';
		}
		
		

		
		$query = "SELECT `id`,`quantity`,`subject`,`assoc_id`,`author`,`reply_count`,`item`,`price`,`item_section` FROM `forum_posts` WHERE `board`='$board' AND `type`='$type' AND `quantity` > '0' AND `reply_to` = '0' ORDER BY `date_sort` DESC";
		$result = $db->query($query);
		
	    /* fetch associative array */
	    while ($row = $result->fetch_object()) 
	    {
	    	switch ($type)
	    	{
	    		
	    		
	    		
	    		case 'trade':
	    			
	    			$price_total = $row->price * $row->quantity;
	    			
	    			$form = html_form("/forum/$board/$row->id/purchase/",populate_quantity_box('quantity',$row->quantity,'',$row->quantity,'ASC').'<input value="purchase" type="submit">','test');
	    			$output .= "<tr>
				      <td><small>$row->item_section</small>/$row->item</td>
				      <td>&nbsp;</td>
				      <td>$row->price</td>
				      <td>$price_total</td>
				      <td><small>".player_lookup($row->assoc_id,$row->author)."</small></td>
				      <td></td>
				    </tr>
				    <tr>
				      <td>&nbsp;</td>
				      <td colspan=5>".$form."</td>
				    </tr>";
	    			break;
	    			
	    			
	    			
	    			
	    		case 'talk':
	    				$read_post_link = ahref("/forum/$board/$row->id/$row->id/",$row->subject);
	    				$poster_link = player_lookup($row->assoc_id,$row->author);
	    				
		    			$output .= "
						    <tr>
						      <td><B>$read_post_link</B></td>
						      <td> - <small>($row->reply_count replies)</small></td>
						      <td> - <small>$poster_link</small></td>
						    </tr>
				
							";
	    			break;
	    	}
	    	
	    }
	
	    /* free result set */
	    $result->close();
		    
		    
		$output .= '</tbody></table><p>';
		$output .= '</p>';
		
		echo $output;
		return true;
		
	}
	
	
	
	
	function display_post($post_id,$master)
	{
		global $db;
		
		$query = "SELECT * FROM `forum_posts` WHERE `id`='$post_id' AND `quantity` > '0'";
		$post_info = $db->return_first_result($query);
		
		//display_var($post_info);
		
		$board = $this->board;
		$board_info = $this->board_info;
		$board_name = $board_info['name'];
		$type = $this->type;
		
		$subject = ''.$post_info['subject'].'';
		$author = player_lookup($post_info['assoc_id'], $post_info['author']);
		$body = nl2br(''.$post_info['body'].'');
		
		$link_board = ahref("/forum/$board/",$board_name);
		
		$html = "<h1>Forum: $link_board: $subject</h1>";
		
		$html .= '<p>';
		
		switch ($type)
		{
			case 'talk':
				$time = return_datetime($post_info['date']);
				if ($post_id <> $master)
				{
				$top = ahref("/forum/$board/$master/$master/",'TOP') . ' :: ';
				}
				else 
				{
					$top = '';
				}
				$html .= "<div align=\"center\" style=\"border:#7e673d 3px DASHED;width:350px;height:20px;overflow:auto;\"><table width=100%><tr><td align=center>$top<b>$subject</b></td><td align=right><small>&nbsp;</small></td></tr></table></div>
				<div style=\"border:#7e673d 3px DASHED;width:350px;height:20px;overflow:auto;\"><table width=100%><tr><td>$time</td><td align=right><small>($author)</small></td></tr></table></div>
						<div style=\"border:#7e673d 3px DASHED;width:350px;height:200px;overflow:auto;\"><p><font face=Helvecta>$body</font></p></div>
";
				break;
				
			case 'trade':
				$quantity = $_REQUEST['quantity'];
				$total_price = $post_info['price'] * $quantity;
				$item = $post_info['item'];
				if ($master == 'purchase')
				{
					$result = transfer_money_user2user(USER_ID,$post_info['assoc_id'],$total_price,'quadrans');
					if ($result)
					{
						log_memory("Money ($total_price quadrans) transfered...");
						$result = get_item(USER_ID,$item,$quantity,$post_info['item_section'],$post_info['item_section'],0);
						if ($result)
						{
							log_memory("Item ($quantity $item(s)) transfered...");
							$result = $this->maintain_advert($post_id,$quantity);
							if ($result)
							{
								log_memory('Transaction Complete!');
							}
							else 
							{
								log_memory('Problem updating advert');
							}
						}
						else 
						{
							log_memory('Problem transferring item.');
						}
						
					}
					else 
					{
						log_memory($error_message);
					}
				}
				$html .= '';
				break;
		}
		
		
		$html .= '</p>';
		
		## debug (uncomment to show debug
		// $html .= log_memory().'</p>';
		
		echo $html;
		
		return true;
	}
	
	
	function display_replies ($post_id, $original = 0)
	{
		global $db;
		
		$board = $this->board;
		$board_info = $this->board_info;
		$type = $this->type;
		
		$header = $board_info['name'];
		$type = $board_info['cat'];
		
		echo "<ul>";

		
		$query = "SELECT `id`,`master`,`subject`,`author`,`assoc_id`,`reply_count` FROM `forum_posts` WHERE `board`='$board' AND `type`='$type' AND `quantity` > '0' AND `reply_to`='$post_id' ORDER BY `date_sort` DESC";
		$result = $db->query($query);
		$row_cnt = $result->num_rows;
		
	    /* fetch associative array */
	    while ($row = $result->fetch_object()) 
	    {
	    	
	    	switch ($type)
	    	{
	    		
	    		
	    		
	    		case 'trade':
	    			$price_total = $row->price * $row->quantity;
	    			$form = html_form("/forum/$board/purchase/$row->id/",populate_quantity_box('quantity',$row->quantity).'<input value="purchase" type="submit">');
	    			$output .= "";
	    			break;
	    			
	    			
	    			
	    			
	    		case 'talk':
	    				$read_post_link = ahref("/forum/$board/$row->id/$row->master/",$row->subject);
	    				$poster_link = player_lookup($row->assoc_id,$row->author);
		    			echo "<li> $read_post_link - $poster_link";
		    			if ($row->reply_count > 0)
		    			{
		    				$this->display_replies($row->id);
		    			}
	    			break;
	    	}
	    	
	    }
	    
	    if ($original == 1 && $row_cnt == 0 && $type == 'talk')
	    	{
	    		echo '<BR><li> <b>No replies!</b><br>';
	    	}
	
	    /* free result set */
	    $result->close();
		    
		    
		echo '</ul>';
		
		//if ($output == '')
		//{
		//	echo '<li><b>No replies!</b>';
		//}
		
		
		return true;
		
	}


	

	
	
	function display_make_post($post_id = 0,$master_id = 0)
	{
		// HTML form to make/submit your advert
		$board = $this->board;
		$type = $this->type;
		
		
		switch ($type)
		{
			case 'talk':
				$html = '<table style="text-align: left; width: 100px;" border="1"
 cellpadding="2" cellspacing="2">
  <tbody>
    <tr>
      <td></td>
      <td>Subject:</td>
      <td><input name="subject"></td>
    </tr>
    <tr>
      <td></td>
      <td>Body:</td>
      <td><textarea cols="35" rows="3"
 name="body"></textarea></td>
    </tr>
    <tr>
      <td></td>
      <td></td>
      <td><input type=hidden name="v1" value="'.$post_id.'"><input type=hidden name="v2" value="'.$master_id.'"><input value="Submit" type="submit"><input
 value="Reset" type="reset"></td>
    </tr>
  </tbody>
</table>
';
				break;
				
			case 'trade':
				$html = 'item: <input type=text name=item> (should be a drop down list)';
				break;
		}
		
		$output = html_form("/forum/$board/$post_id/",$html);
		echo $output;
		return true;
	}
	
	
	function validate_advert($advert_id,$quantity)
	{
		// check the submitted advert is valid
		
		global $db;
		
		$sql = "SELECT * FROM `user_items_trade` WHERE `advert_id`='$advert_id'";
		$trade_info = $db->return_first_result($sql);
		
		if ($trade_info['quantity'] <= $quantity)
		{
			return true;
		}
		else 
		{
			return false;
		}
	}
	
	function validate_items($assoc_id,$item, $quantity)
	{
		// check the submitted advert is valid
		
		global $db;
		
		$sql = "SELECT * FROM `user_items` WHERE `item`='$item' AND `assoc_id`='$assoc_id'";
		$trade_info = $db->return_first_result($sql);
		
		if ($trade_info['quantity'] <= $quantity)
		{
			return true;
		}
		else 
		{
			return false;
		}
	}
	
	function post($post_id = '0',$master = '0')
	{
		// add the advert/message to forum_posts
		
		global $db, $user_info;
		
		$type = $this->type;
		
		switch ($type)
		{
			case 'trade':
				$insert = $db->prepare("INSERT INTO `forum_posts` (`assoc_id`,`subject`,`etc`) VALUES (?, ?, ?, ?, ?, ?)");
        		$insert->bind_param('sissss', $assoc_id, $subject, $etc);
				$check = $this->item_lock();
				$subject = '';
				break;
			case 'talk':
				$check = true;
				$insert = $db->prepare("INSERT INTO `forum_posts` (`board`,`quantity`,`assoc_id`,`author`,`type`,`master`,`reply_to`,`subject`,`body`,`date`,`date_sort`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)") or die ($db->error);
				$insert->bind_param('siissiissii', $board,$quantity,$assoc_id,$author,$type,$master,$reply_to,$subject,$body,$time, $time);
				$subject = $_REQUEST['subject'];
				$body = $_REQUEST['body'];
				$board = $this->board;
				$quantity = 1;
				$author = $user_info[USER_ID]['username'];
				$type = $type;
				$assoc_id = USER_ID;
				if ($post_id > 0)
				{
					$reply_to = $post_id;
					$master = $master;
					if ($reply_to == $master)
					{
						$this->update_reply_count($master);
					}
					else 
					{
						$this->update_reply_count($master);
						$this->update_reply_count($reply_to);
					}
				}
				else 
				{
					$reply_to = 0;
					$master = 0;
				}
				$time = NOW;
				break;
		}
		
		if ($check == true)
		{
			
			$result = $insert->execute();
			# DEBUG
			//echo $db->error;
			//display_var(get_defined_vars());
			return $insert->insert_id; 
		}
		else 
		{
			return false;
		}
	}
	
	
	function update_reply_count($post_id)
	{
		global $db;
		
		$time = NOW;
		
		return $db->query("UPDATE `forum_posts` SET `reply_count`=reply_count+'1',`date_sort`='$time' WHERE `id`='$post_id'");
	}
	
	function item_lock($assoc_id,$item,$quantity)
	{
		// move the items from user_items to user_items_trade so nobody can touch them
		
		global $db;
		
		$item_check = $this->validate_items($assoc_id,$item,$quantity);
		if ($item_check)
		{
			// user has enough of item
			
			$sql_update = "UPDATE `user_items` SET `quantity`=quantity-'$quantity' WHERE `item`='$item' AND `assoc_id`='$assoc_id'";
			$result_update = $db->query($sql_update);
			
			$sql_insert = "INSERT INTO `user_items_trade` (``) VALUES ('')";
			$result_insert = $db->query($sql_insert);
			
			if ($result_insert && $result_update)
			{
				return true;
			}
			else 
			{
				return false;
			}
		}
		else 
		{
			// user doesn't have enough of item
			return false;
		}
	}
	
	function maintain_advert($advert_id,$quantity)
	{
		// update or delete the post after purchase
		
		global $db;
		
		$sql = "UPDATE `forum_posts` SET `quantity`=quantity-'$quantity' WHERE `id`='$advert_id'";
		return $db->query($sql);
	}
	
	function send_news($destination)
	{
		// send news to $destination
	}
	
	
	
	
	function detect_new_post()
	{
		if (strlen(@$_REQUEST['subject']) > 0 && strlen(@$_REQUEST['body']) > 0)
		{
			return true;
		}
		else 
		{
			return false;
		}
	}
	
	
	
	
}

?>